package com.example.supplychainbackend.controller;

import com.example.supplychainbackend.controller.dto.DataResponse;
import com.example.supplychainbackend.controller.dto.blockchain.UploadInfo;
import com.example.supplychainbackend.controller.dto.logistic.DomesticLogisticDto;
import com.example.supplychainbackend.controller.dto.logistic.ForeignLogisticDto;
import com.example.supplychainbackend.controller.dto.user.UserSigDto;
import com.example.supplychainbackend.service.LogisticService;
import com.example.supplychainbackend.tool.JwtUtil;
import jakarta.transaction.Transactional;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/logistic")
@AllArgsConstructor
public class LogisticController {
    private final LogisticService logisticService;

    @PostMapping("/domestic/{id}/chain")
    public DataResponse<UploadInfo> uploadDomesticLogistic(@PathVariable String id){
        UploadInfo res = logisticService.uploadDomesticLogistic(id);
        return DataResponse.success(res);
    }
    @PostMapping("/domestic")
    public DataResponse<String> createDomesticLogistic(@RequestHeader("Authorization") String authHeader,
                                                       @RequestBody DomesticLogisticDto form) {
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        form.setUserName(userSigDto.getUserName());
        form.setUserId(userSigDto.getUserId());
        form.setEnterpriseName(userSigDto.getEnterpriseName());
        form.setEnterpriseId(userSigDto.getEnterpriseId());
        String lineNumber = logisticService.createDomesticLogistic(form);
        return DataResponse.success(lineNumber);
    }

    @PutMapping("/domestic/{id}")
    public DataResponse<DomesticLogisticDto> updateDomesticLogistic(@RequestHeader("Authorization") String authHeader,
                                                                    @PathVariable String id, @RequestBody DomesticLogisticDto form) {
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        form.setUserName(userSigDto.getUserName());
        form.setUserId(userSigDto.getUserId());
        form.setEnterpriseName(userSigDto.getEnterpriseName());
        form.setEnterpriseId(userSigDto.getEnterpriseId());
        DomesticLogisticDto res = logisticService.updateDomesticLogistic(id, form);
        return DataResponse.success(res);
    }

    @GetMapping("/domestic/list")
    public DataResponse<List<DomesticLogisticDto>> listDomesticLogistics() {
        List<DomesticLogisticDto> res = logisticService.getDomesticLogistics();
        return DataResponse.success(res);
    }

    @GetMapping("/domestic/{id}")
    public DataResponse<DomesticLogisticDto> getDomesticLogistic(@PathVariable String id) {
        DomesticLogisticDto res = logisticService.getDomesticLogistic(id);
        return DataResponse.success(res);
    }

    @DeleteMapping("/domestic/{id}")
    @Transactional
    public DataResponse<Long> deleteDomesticLogistic(@PathVariable String id) {
        logisticService.deleteDomesticLogistic(id);
        return DataResponse.success(1L);
    }
    @PostMapping("/foreign/{id}/chain")
    public DataResponse<UploadInfo> uploadForeignLogistic(@PathVariable String id){
        UploadInfo res = logisticService.uploadForeignLogistic(id);
        return DataResponse.success(res);
    }
    @PostMapping("/foreign")
    public DataResponse<String> createForeignLogistic(@RequestHeader("Authorization") String authHeader,
                                                      @RequestBody ForeignLogisticDto form) {
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        form.setUserName(userSigDto.getUserName());
        form.setUserId(userSigDto.getUserId());
        form.setEnterpriseName(userSigDto.getEnterpriseName());
        form.setEnterpriseId(userSigDto.getEnterpriseId());
        String foreignNumber = logisticService.createForeignLogistic(form);
        return DataResponse.success(foreignNumber);
    }

    @PutMapping("/foreign/{id}")
    public DataResponse<ForeignLogisticDto> updateForeignLogistic(@RequestHeader("Authorization") String authHeader,
                                                                  @PathVariable String id, @RequestBody ForeignLogisticDto form) {
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        form.setUserName(userSigDto.getUserName());
        form.setUserId(userSigDto.getUserId());
        form.setEnterpriseName(userSigDto.getEnterpriseName());
        form.setEnterpriseId(userSigDto.getEnterpriseId());
        ForeignLogisticDto res = logisticService.updateForeignLogistic(id, form);
        return DataResponse.success(res);
    }

    @GetMapping("/foreign/list")
    public DataResponse<List<ForeignLogisticDto>> listForeignLogistics() {
        List<ForeignLogisticDto> res = logisticService.getAllForeignLogistics();
        return DataResponse.success(res);
    }

    @GetMapping("/foreign/{id}")
    public DataResponse<ForeignLogisticDto> getForeignLogistic(@PathVariable String id) {
        ForeignLogisticDto res = logisticService.getForeignLogistic(id);
        return DataResponse.success(res);
    }

    @DeleteMapping("/foreign/{id}")
    @Transactional
    public DataResponse<Long> deleteForeignLogistic(@PathVariable String id) {
        return DataResponse.success(logisticService.deleteForeignLogistic(id));
    }

    @ExceptionHandler(Exception.class)
    public DataResponse<Exception> logisticErrorHandler(Exception e) {
        return DataResponse.error(500,e);
    }

}
